package Leetcode;

import java.util.HashMap;
import java.util.Map;

public class copyRandomList {
    public Node copyRandomList(Node head) {
        if (head == null){
            return null;
        }
        Map<Node, Node> nodeNodeHashMap = new HashMap<>();
        Node node = head;
        while (node != null){
            Node nodeNew = new Node(node.val);
            nodeNodeHashMap.put(node, nodeNew);
            node = node.next;
        }
        node = head;
        while (node != null){
            Node nodeNew = nodeNodeHashMap.get(node);
            nodeNew.next = nodeNodeHashMap.get(node.next);
            nodeNew.random = nodeNodeHashMap.get(node.random);
            node = node.next;
        }
        return nodeNodeHashMap.get(head);
    }
}
